package com.baymax.springboot.cli.controller;

import com.baymax.springboot.cli.config.datasource.IDataSourceConfig;
import com.baymax.springboot.cli.mapper.HdeUserMapper;
import com.baymax.springboot.cli.model.HdeUser;
import com.baymax.springboot.cli.model.HdeUserExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/restful")
public class RestfulController {

    @Resource
    private HdeUserMapper hdeUserMapper;

    @Resource
    IDataSourceConfig iDataSourceConfig;

    /**
     * 访问路径http://localhost:8880/restful/users
     * @return
     */
    @RequestMapping(value="/users", method=RequestMethod.GET)
    public List<HdeUser> query() {
        iDataSourceConfig.setup();
        HdeUserExample example = new HdeUserExample();
        example.setDynamicColumns(new String[]{"id", "name"});
        return hdeUserMapper.selectByExample(example);
    }

    /**
     * 访问路径： http://localhost:8880/restful/user/10
     * 返回值： {"id":10,"gmtCreated":"2020-03-06T04:38:05","creator":"Huxiao",
     *          "gmtModified":"2020-03-06T04:38:05","modifier":"Huxiao","isDeleted":"n",
     *          "name":"9","age":68,"salary":4222.00,"yearOfWorking":25,"tags":"tags","version":1,"remark":""}
     *
     * @param id
     * @return
     */
    @RequestMapping(value="/user/{id:\\d+}",method=RequestMethod.GET)//如果希望对传递进来的参数作一些限制，使用正则表达式
    public HdeUser user(@PathVariable String id) {
        if(!StringUtils.isNumeric(id)) {
            return null;
        }
        //return hdeUserMapper.selectByPrimaryKey(Integer.parseInt(id));
        return null;
    }

    /**
     * 模糊查询
     * http://localhost:8880/restful/users/o
     *
     * 返回
     * [{"id":3,"gmtCreated":"2020-03-06T03:55:54","creator":"Huxiao","gmtModified":"2020-03-06T03:55:54",
     * "modifier":"Huxiao","isDeleted":"n","name":"O","age":25,"salary":6201.00,"yearOfWorking":11,"tags":"tags",
     * "version":1,"remark":null},{"id":5,"gmtCreated":"2020-03-06T04:11:30","creator":"Huxiao",
     * "gmtModified":"2020-03-06T04:11:30","modifier":"Huxiao","isDeleted":"n","name":"hIVjFZo","age":98,
     * "salary":4373.00,"yearOfWorking":18,"tags":"tags","version":1,"remark":null},
     * {"id":13,"gmtCreated":"2020-03-06T04:38:07","creator":"Huxiao","gmtModified":"2020-03-06T04:38:07",
     * "modifier":"Huxiao","isDeleted":"n","name":"ObG","age":12,"salary":2986.00,"yearOfWorking":1,
     * "tags":"tags","version":1,"remark":null},{"id":21,"gmtCreated":"2020-03-06T07:25:09","creator":"no_tx",
     * "gmtModified":"2020-03-06T07:25:09","modifier":"no_tx","isDeleted":"n","name":"no_tx","age":26,
     * "salary":6000.00,"yearOfWorking":10,"tags":"tags","version":1,"remark":null}]
     *
     * @RequestParam 和 @PathVariable 注解是用于从request中接收请求的，两个都可以接收参数，
     * 关键点不同的是
     * @RequestParam 是从request里面拿取值，
     * @PathVariable 是从一个URI模板里面来填充
     *
     * @param name
     * @return
     */
    @RequestMapping(value="/users/{name}",method=RequestMethod.GET)
    public List<HdeUser> users(@PathVariable String name) {
        HdeUserExample example = new HdeUserExample();
        HdeUserExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotBlank(name)){
            criteria.andNameLike('%' + name + '%');
        }

        example.setDynamicColumns(new String[]{"id", "name"});

        return hdeUserMapper.selectByExample(example);
    }

    /**
     * 这里是没有使用自定义分页插件时，不支持分页查询的情况
     *
     * @param name
     * @param currentPage
     * @return
     */
    @RequestMapping(value="/users/{name}/{page:\\d+}",method=RequestMethod.GET)
    public List<HdeUser> users(@PathVariable String name, @PathVariable Integer currentPage) {
        int rows = 5;
        int start = (currentPage - 1) * rows;

        HdeUserExample example = new HdeUserExample();
        /**
         * 要实现此功能需要在HdeUserExample中添加属性，然后在mapper中增加limit #{startRow},#{pageSize}
         */
        //分页查询中的一页数量
        //example.setPageSize(rows);
        //开始查询的位置
        //example.setStartRow(start);
        return hdeUserMapper.selectByExample(example);
    }

    /**
     * 经过使用自定义的mybatis-generator插件可以生成HdeUserExample中包含offset和limit属性
     * 以此能支持分页查询
     *
     * @return
     */
    @RequestMapping(value="/usersByPage",method=RequestMethod.GET)
    public List<HdeUser> usersByPage() {
        HdeUserExample example = new HdeUserExample();
        example.setOffset(1); // offset
        example.setLimit(10); // page size limit
        return hdeUserMapper.selectByExample(example);
    }

    /**
     * http://localhost:8880/restful/users/count
     *
     * 可以成功返回19
     *
     * 居然没有和@RequestMapping(value="/users/{name}"的方法冲突！
     *
     * @return
     */
    @RequestMapping(value="/users/count",method=RequestMethod.GET)
    public long count() {
        HdeUserExample example = new HdeUserExample();
        return hdeUserMapper.countByExample(example);
    }
}
